package com.hand.wyc24548.app.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hand.wyc24548.app.service.SoHeaderService;
import com.hand.wyc24548.domain.entity.SoHeader;
import com.hand.wyc24548.domain.entity.SoLine;
import com.hand.wyc24548.domain.repository.SoHeaderRepository;
import com.hand.wyc24548.domain.repository.SoLineRepository;

import io.choerodon.core.exception.CommonException;

/**
 * @Author wangyc
 * @create 2019/8/9 9:18
 */

@Service
public class SoHeaderServiceImpl implements SoHeaderService {

    private final SoHeaderRepository soHeaderRepository;
    private final SoLineRepository soLineRepository;

    public SoHeaderServiceImpl(SoHeaderRepository soHeaderRepository, SoLineRepository soLineRepository) {
        this.soHeaderRepository = soHeaderRepository;
        this.soLineRepository = soLineRepository;
    }

    /**
     * 新增订单头
     * @param soHeader
     * @return
     */
/*    @Override
    @Transactional(rollbackFor = Exception.class)
    public SoHeader create(SoHeader soHeader) {
        soHeaderRepository.insertSoHeader(soHeader);
        return soHeader;
    }*/

    /**
     * 删除订单头同时删除订单行
     * @param soHeaderId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long soHeaderId) {
        SoHeader soHeader=soHeaderRepository.selectByPrimaryKey(soHeaderId);
        if (soHeader==null){
            throw new CommonException("hodr.so.header.notFound");
        }
        //删除订单头
        soHeaderRepository.deleteByPrimaryKey(soHeaderId);
        //删除与订单头关联的订单行
        /*List<SoLine> soLines=soLineRepository.selectBySoHeaderId(soHeaderId);
        if (CollectionUtils.isNotEmpty(soLines)){
            soLineRepository.batchDelete(soLines);
        }*/
    }

}
